﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MarkDownFileSplitter.Meilisearch
{
    public partial class FormSetting_Meilisearch : Form
    {
        private Func<Task> m_resetAction;
        public FormSetting_Meilisearch(Func<Task> resetAction = null)
        {
            InitializeComponent();
            m_resetAction = resetAction;
        }

        private void FormSetting_Meilisearch_Load(object sender, EventArgs e)
        {
            var setting = GlobalSetting.Instance;
            this.textBox_MeilisearchUrl.Text = setting.MeilisearchUrl;
            this.textBox_MeilisearchAPIKey.Text = setting.MeilisearchAPIKey;
        }

        private async void button_saveConfig_Click(object sender, EventArgs e)
        {
            var setting = GlobalSetting.Instance;
            setting.MeilisearchUrl = this.textBox_MeilisearchUrl.Text;
            setting.MeilisearchAPIKey = this.textBox_MeilisearchAPIKey.Text;
            setting.SaveToFile();

            MeilisearchUploader.Instance.UpdateSetting();

            MessageBox.Show("保存成功");

            if (m_resetAction != null)
            {
                await m_resetAction();
            }

            DialogResult = DialogResult.OK;
        }
    }
}
